package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardRoomConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


/**
 * @Description
 * @Author cwq
 * @Data 2021/12/9 11:33
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_MATCH_SUCCESS_FROM_CROSS, desc = "水浒叶子牌 匹配成功 跨服返回区服 协议")
public class ShuiHuYeZiCardMatchSuccessFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiCardMatchSuccessGameToServerMsg crossMsg = ShuiHuYeZiCardProto.ShuiHuYeZiCardMatchSuccessGameToServerMsg.parseFrom(packet.getBytes());
        ShuiHuYeZiCardRoomConfig roomConfig = ShuiHuYeZiCardMgr.getConfig().getRoomConfigMap().get(crossMsg.getRoomType());
        ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
        if(roomConfig != null){
            int times = shuiHuYeZiCardModule.getUserData().getJoinRoomTimesMap().getOrDefault(roomConfig.getId(),0);
            if(times >= roomConfig.getFreePlay()){
                Property consume = PropertyHelper.parseStringToProperty(roomConfig.getConsume());
                // 解除预扣除
                player.getModule(PreReduceModule.class).restorePreReduce(consume);
                // 扣道具
                player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.ShuiHuYeZiCardActivity,eLogMoneyType.ShuiHuYeZiCardActivityPvpConsume);
            }
        }
        shuiHuYeZiCardModule.addJoinRoomTimes(crossMsg.getRoomType(),1);
        shuiHuYeZiCardModule.setStatus(0);
        if(shuiHuYeZiCardModule.getPvpRoomId() != 0){
            ShuiHuYeZiCardMgr.getLogger().info("水浒叶子牌 出现了。玩家已有pvp roomId ,却又可以新开了房间。userId:{},oldPvpRoomId :{},new pvp roomId:{}",player.getUserId(),shuiHuYeZiCardModule.getPvpRoomId(),crossMsg.getRoomType());
        }
        shuiHuYeZiCardModule.setPvpRoomId(crossMsg.getRoomType());

        player.sendPacket(YanQuMessageUtils.buildMessage(ClientProtocol.U_SHUIHUYEZICARD_BROADCAST, crossMsg.getBroadcast()));
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
